'''
Created on Oct 12, 2012

@author: jake
'''

import sys
import urllib
import xbmcplugin #@UnresolvedImport
import xbmcgui #@UnresolvedImport
import xbmc #@UnresolvedImport

class UI:
    def ui(self):
        i = 'o'
        
    def endOfDirectory(self, sortMethod = 'none'):
        # set sortmethod to something xbmc can use
        if sortMethod == 'title':
            xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_LABEL)
            xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_VIDEO_RATING)
        elif sortMethod == 'none':
            xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_NONE)

        dontAddToHierarchy = False
        xbmcplugin.endOfDirectory(handle = int(sys.argv[1]), updateListing = dontAddToHierarchy)
        
    def addItem(self, info, extraInfo=None, isFolder=True):
        info.setdefault('TITLE', 'None')
        info.setdefault('MODE', 'None')
        info.setdefault('URL', 'None')
        info.setdefault('FASTBROWSE', 'True')
        
        title=info['TITLE']
        mode=info['MODE']
        url=info['URL']
        fastBrowse=info['FASTBROWSE']
        
        u = sys.argv[0]+\
            '?url='+urllib.quote_plus(url)+\
            '&mode='+urllib.quote_plus(mode)+\
            '&name='+urllib.quote_plus(title)+\
            '&fastbrowse='+urllib.quote_plus(fastBrowse)
        
        listItem=xbmcgui.ListItem(title)
        
        if extraInfo is not None:
            listItem.setIconImage(extraInfo['ICON'])
            listItem.setThumbnailImage(extraInfo['ICON'])
            ins = {'genre':extraInfo['GENRE'], 'episode': extraInfo['EPISODES'],'mpaa':extraInfo['AGERATING'], 'plot':extraInfo['ABOUT']}
            listItem.setInfo('video', ins)
        
        
        if isFolder:
            listItem.setProperty("IsPlayable", "false")
        else:
            listItem.setProperty("IsPlayable", "true")
        
        xbmcplugin.addDirectoryItem(int(sys.argv[1]), url=u, listitem=listItem, isFolder=isFolder)
        
    def showCategories(self):
        self.addItem({'TITLE':"All Anime series", 'MODE':'ALL'})
        self.addItem({'TITLE':"List Anime A-Z", 'MODE':'A2Z'})
        self.addItem({'TITLE':"List Anime A-Z (with details)", 'MODE':'A2Z', 'FASTBROWSE':'False'})
        self.endOfDirectory()
        
    def playEpisode(self, title, url):
        item = xbmcgui.ListItem( label = title, label2 = title)
        item.setInfo("video", infoLabels={ "Title": title })
        xbmc.Player(xbmc.PLAYER_CORE_DVDPLAYER).play(url, item)
        
    def showDialog(self, message, title="Warning"):
        dialog = xbmcgui.Dialog()
        dialog.ok(title, message)
